package com.zhixun.zhlq.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;

import com.zhixun.zhlq.dao.SysLogsMapper;
import com.zhixun.zhlq.entity.SysLogsEntity;
import com.zhixun.zhlq.entity.vo.SysLogsVO;
import com.zhixun.zhlq.service.SysLogsService;
import com.zhixun.zhlq.utils.OsAndBrowserUtil;
import com.zhixun.zhlq.utils.PageUtil;
import com.zhixun.zhlq.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @ClassName: SysLogsServiceImpl
 * @Author: eollse-wj
 * @Date: 2019-12-20 16:19:04
 * @Description:
 */
@Service
public class SysLogsServiceImpl extends ServiceImpl<SysLogsMapper, SysLogsEntity> implements SysLogsService {
    @Autowired
    private SysLogsMapper sysLogsMapper;

    @Override
    public PageInfo<SysLogsVO> getList(Map<String, Object> param) {
        PageUtil.pageLimit15(param);
        List<SysLogsVO> list = sysLogsMapper.getList(param);
        return new PageInfo<>(list);
    }

    @Override
    public Map<String, Object> statistics() {
        Map<String, Object> data = new HashMap<>(2);
        //今日登录数
        int todayLoginNum = sysLogsMapper.todayLoginNum();
        data.put("todayLoginNum", todayLoginNum);
        //小程序总活跃度
        int smallProLoginNum = sysLogsMapper.smallProLoginNum();
        data.put("smallProLoginNum", smallProLoginNum);
        return data;
    }

    @Override
    public int add(HttpServletRequest request, String action, String msg, Long userId, String name) {
        String clientOrigin = "99";
        String ip = null;
        String os = null;
        String browser = null;
        if (request != null) {
            OsAndBrowserUtil osAndBrowser = OsAndBrowserUtil.getOSAndBrowser(request);
            if (!StringUtil.isEmpty(request.getHeader("clientOrigin"))){
                clientOrigin = request.getHeader("clientOrigin");
            }
            ip = osAndBrowser.getIP();
            os = osAndBrowser.getOs();
            browser = osAndBrowser.getBrowser();
        }
        SysLogsEntity logs = new SysLogsEntity(action, msg,ip, os, browser, clientOrigin, userId,name);
        return sysLogsMapper.insert(logs);
    }

    @Override
    public PageInfo<SysLogsVO> listStatistics(Map<String, Object> param) {
        PageUtil.pageLimit15(param);
        List<SysLogsVO> list = sysLogsMapper.listStatistics(param);
        return new PageInfo<>(list);
    }
}